package TP1;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.HashSet;

public class IndexMultinomial implements IndexInterface {
	public HashMap<String, Pair> contenuDoc = new HashMap<String, Pair>();
	public int nbDoc, nbMotDocHam, nbMotDocSpam;
	public static float EPSILON = 0.00000001f;

	public IndexMultinomial() {
		nbDoc = 0;
		nbMotDocHam = 0;
		nbMotDocSpam = 0;
	}

	public void addDocToIndex(File document, boolean isHam) {
		nbDoc++;
		try {
			InputStream ips = new FileInputStream(document);
			InputStreamReader ipsr = new InputStreamReader(ips);
			BufferedReader br = new BufferedReader(ipsr);
			String ligne;
			String[] mots;
			while ((ligne = br.readLine()) != null) {
				mots = ligne.split(" ");
				for (String mot : mots) {
					if (isHam) {
						nbMotDocHam++;
					} else {
						nbMotDocSpam++;
					}
					if (contenuDoc.containsKey(mot)) {
						if (isHam) {
							contenuDoc.get(mot).ham++;
						} else {
							contenuDoc.get(mot).spam++;
						}
					} else {
						if (isHam) {
							contenuDoc.put(mot, new Pair(1, 0));
						} else {
							contenuDoc.put(mot, new Pair(0, 1));
						}
					}
				}
			}
			br.close();
		} catch (Exception e) {
			System.out.println(e.toString());
		}
	}

	public float evalueDocument(String fichierName) {
		File fichier = new File(fichierName);
		return evalueDocument(fichier);
	}

	public void MakeIndex() {
		for (String motCorpus : contenuDoc.keySet()) {
			contenuDoc.get(motCorpus).ham /= nbMotDocHam;
			contenuDoc.get(motCorpus).spam /= nbMotDocSpam;
			if (contenuDoc.get(motCorpus).ham == 0) {
				contenuDoc.get(motCorpus).ham = EPSILON;
			} else if (contenuDoc.get(motCorpus).ham == 1) {
				contenuDoc.get(motCorpus).ham = 1 - EPSILON;
			}

			if (contenuDoc.get(motCorpus).spam == 0) {
				contenuDoc.get(motCorpus).spam = EPSILON;
			} else if (contenuDoc.get(motCorpus).spam == 1) {
				contenuDoc.get(motCorpus).spam = 1 - EPSILON;
			}
		}
	}

	public float evalueDocument(File fichier) {
		HashSet<String> motTrouver = new HashSet<String>();
		try {
			InputStream ips = new FileInputStream(fichier);
			InputStreamReader ipsr = new InputStreamReader(ips);
			BufferedReader br = new BufferedReader(ipsr);
			String ligne;
			String[] mots;
			while ((ligne = br.readLine()) != null) {
				mots = ligne.split(" ");
				for (String mot : mots) {
					if (!motTrouver.contains(mot)) {
						motTrouver.add(mot);
					}
				}
			}
		} catch (Exception e) {
			System.out.println(e.toString());
		}

		float ratioHam = 0, ratioSpam = 0;
		for (String motCorpus : contenuDoc.keySet()) {
			if (motTrouver.contains(motCorpus)) {
				ratioHam += Math.log(contenuDoc.get(motCorpus).ham);
				ratioSpam += Math.log(contenuDoc.get(motCorpus).spam);
			}
		}
		// TODO Auto-generated method stub
		return ratioSpam - ratioHam;
	}
}
